function! plugin#asynctasks#load() abort
    if !exists('g:loaded_asynctasks')
        packadd asynctasks.vim
        let g:loaded_asynctasks = 1
    endif
    return exists('g:loaded_asynctasks')
endfunction

function! plugin#asynctasks#init() abort
    call plugin#asynctasks#variables()
    call plugin#asynctasks#keymap()
endfunction 

function! plugin#asynctasks#keymap() abort
    let g:leader_keymap = get(g:, 'leader_keymap', { 'name'  : '+leader' })
    let g:async_keymap = get(g:leader_keymap, 'a', { 'name'  : '+async' })
    let g:leader_keymap.a = g:async_keymap
    let g:asynctask_keymap = get(g:async_keymap, 't', { 'name'  : '+asynctask'           , 
                \               'e'     :   [ 'AsyncTaskEdit'       , 'edit'           ],
                \               'g'     :   [ 'AsyncTaskLast'       , 'last'           ],
                \               'l'     :   [ 'AsyncTaskList'       , 'list'           ],
                \               'm'     :   [ 'AsyncTaskMacro'      , 'macro'          ],
                \               'p'     :   [ 'AsyncTaskProfile'    , 'profile'        ],
                \               'v'     :   [ 'AsyncTaskEnv'        , 'environment'    ],
                \               'r'     :   [ 'AsyncTask file-run'  , 'file-run'       ],
                \               'b'     :   [ 'AsyncTask file-build', 'file-build'     ],
                \             })
    let g:leader_keymap.a.t = g:asynctask_keymap
    let g:cmake_keymap = { 'name': '+cmake', 
                \ 'i': [':AsyncTask cmake-init<cr> ', 'init'],
                \ 'b': [':AsyncTask cmake-build<cr>', 'build'],
                \ 't': [':AsyncTask cmake-test<cr> ', 'test'],
                \ 'c': [':AsyncTask cmake-clean<cr>', 'clean'],
                \ }
    let g:leader_keymap.a.b = g:cmake_keymap
    let g:asyncrun_open = get(g:, 'asyncrun_open', 4)

    noremap <silent>    <f5> :AsyncTask file-run<cr>
    noremap <silent>    <f6> :AsyncTask file-build<cr>
    noremap <silent>    <f7> :AsyncTask project-run<cr>
    noremap <silent>    <f8> :AsyncTask project-build<cr>
endfunction 

function! plugin#asynctasks#variables() abort
    let g:asyncrun_rootmarks = ['.git', '.svn', '.root', '.project', '.hg']
    let g:asynctasks_config_name = ['.tasks', '.git/tasks.ini', '.svn/tasks.ini']
    let g:asynctasks_rtp_config = "asynctasks.ini"
    let g:asynctasks_extra_config = [
                \ '~/vimfiles/asynctasks.ini',
                \ ]
    " let g:asynctasks_term_pos = "bottom"
    " let g:asynctasks_term_pos = 'tab'
    let g:asynctasks_term_pos = 'right'
    let g:asynctasks_term_listed = 0
    let g:asynctasks_term_cols = 80    " 内置终端的宽度（使用水平分割时）。
    let g:asynctasks_term_rows = 10    " 内置终端的高度（使用垂直分割时）。
    let g:asynctasks_term_focus = 0    " 设置成 `0` 可以在使用分屏内置终端的时候，避免焦点切换。
    let g:asynctasks_term_reuse = 1    " 设置成 `1` 可以复用 tab 类型的内置终端。
    let g:asynctasks_term_hidden = 1   " 设置成 `1` 的话，所有内置终端的 buffer 会将 `bufhidden` 初始化成 `hide`。
    let g:asynctasks_template = 1      " 设置成 `0` 的话，新建配置文件时就不使用模板了。
    let g:asynctasks_confirm = 1       " 设置成 `0` 的话，使用 `:AsyncTaskEdit` 时就不需要你确认文件名了。
endfunction 

